-module(hello).
-compile(export_all).
-record(person, {firstName="",
                 lastName="",
                 age}).
-ifdef(debug).
-define(TRACE(X), io:format("TRACE ~p:~p ~p~n", [?MODULE, ?LINE, X])).
-else.
-define(TRACE(X), void).
-endif.

trace(X) ->
    ?TRACE(X),
    X.

print() -> 
    io:format("~15w ~w ~n", [hello, world]),
    [?FILE, ?MODULE, ?LINE].

fac(I) -> I*I.

for(I, Max, F, Sum) when I < Max ->
    for(I+1, Max, F, Sum+F(I));
for(_I, _Max, _F, Sum) ->
    Sum.

add(Max) ->
    for(0, Max, fun fac/1, 0).

qsort([]) -> [];
qsort([Pivot|T]) ->
    qsort([X||X <- T, X =< Pivot]) ++
    [Pivot] ++
    qsort([X||X <- T, X > Pivot]).

perms([]) -> [[]];
perms(L) ->
    [[H|T] || H <- L, T <- perms(L--[H])].

test_record() ->
    Person = #person{firstName="Rip",
                     lastName="Van Winkle",
                     age=793
                    },
    birthday(Person).

birthday(X = #person{age=N}) ->
    X#person{age=N+1}.

month_length(Year, Month) ->
    Leap = if
        trunc(Year/400)*400 == Year ->
            leap;
        trunc(Year/100)*100 == Year ->
            no_leap;
        trunc(Year/4)*4 == Year ->
            leap;
        true ->
            no_leap
    end,

    case Month of
        jan -> 31;
        feb when Leap == leap -> 29;
        feb -> 28;
        mar -> 31;
        apr -> 30;
        may -> 31;
        jun -> 30;
        jul -> 31;
        aug -> 31;
        sep -> 30;
        oct -> 31;
        nov -> 30;
        dec -> 31
    end.
